import axios from 'axios'
import { ElMessage } from 'element-plus'
const request = axios.create()
export default request
// 请求拦截
request.interceptors.request.use(
  (config: any) => {
   
    // // 改为获取cookie中的token
    // config.headers.Authorization = config.headers.Authorization || getCookie()
    // // console.log('请求拦截器', config.url);
    // if (!notNeedHeader(config.url)) {// 需要头
    //   const currentOrg = getLocalStorage(store.currentServiceName)
    //   if (!currentOrg) return config
    //   config.headers['x-meshcloud-resource-id'] = currentOrg.resource_id
    //   config.headers['x-meshcloud-company-id'] = currentOrg.company_id
    // }
    const token = localStorage.getItem('token')
    config.headers.token = token
    return config
  },
  (error) => {
    // 请求错误
    // console.log('请求错误做的事');
    return Promise.reject(error)
  }
)

// 响应拦截
request.interceptors.response.use(
  (res) => {
    // console.log('响应拦截器',res)
    if (res.status === 200) {
      return Promise.resolve(res.data)
    } else {
      return Promise.reject(res.data)
    }
  },
  (error) => {

    // 响应错误
    if (error && error.response.status === 401) {
      // const callBack = window.location.href
      window.location.href = '/login'
      return 
    }
    const reg = /^5/g
    if (error && reg.test(error.response.status)) {
      showMessage()
    }
    return Promise.reject(error)
  }
)

const showMessage = () => {
  ElMessage.error('亲，系统遇到问题了，正在抓紧解决，请您稍后再试哦～')
}
